<template>
  <div class="login">
    <van-nav-bar title="注册/登录" left-arrow @click-left="onClickLeft" />
    
    <van-form @submit="onSubmit">
  <van-field
    v-model="username"
    name="username"
    label="手机号"
    left-icon="phone-o"
    placeholder="请输入手机号"
    :rules="[{ required: true, message: '请输入手机号' }]"
  />
  <van-field
    v-model="password"
    type="password"
    name="password"
    label="密码"
    left-icon="eye-o"
    placeholder="请输入密码"
    
  />
 
  <div style="margin: 30px 30px;">
					<van-button round block type="primary" size="normal" :square="true" native-type="submit">确定</van-button>
					<router-link to="/reg" class="link">还没账号? 立即注册</router-link>
				</div>
</van-form>
  </div>
  
</template>

<script>
export default {
    data() {
    return {
      username: '',
      password: '',
    };
  },
  mounted() {
    
  },
    methods: {

        onClickLeft() {
            this.$router.back()
        },
        onSubmit(values) {
      console.log('submit', values);
      this.$http.post("/user/login",values).then(res=>{
        console.log(res);
        if(res.data.code==0){
          let{token,uid,userInfo}=res.data;
              localStorage.setItem('token',token)
              localStorage.setItem('uid',uid)
              localStorage.setItem('userInfo',JSON.stringify(userInfo))

              this.$store.commit('change',{key:'uid',value:uid})
              this.$store.commit('change',{key:'token',value:token})
              this.$store.commit('change',{key:'userInfo',value:userInfo})
              this.$store.commit('change',{key:'islogin',value:true})
              
              // this.$store.commit('changeuid',uid)

              setTimeout(v=>{
                this.$router.back()
              },500)
        }else{
          this.$toast(res.data.msg||'用户不存在');
        }
      })
    },
    
  },
};
</script>

<style lang="less" scoped>


.van-button--mini{
    display: flex;
    top: -110px;
    right: -280px;
    background-color: rgb(237, 230, 230);
    border: none;
    border-radius: 30px;
}
.van-button__text{
    color: #323233;
}
.van-button--round{
    background: #07c160;
    border-radius: 0;
    border: none;
}
</style>

    
